package cn.zhaoyuening.hnistalumnirecord.service.impl;

import cn.zhaoyuening.hnistalumnirecord.domain.AdminUser;
import cn.zhaoyuening.hnistalumnirecord.service.AdminUserService;
import cn.zhaoyuening.hnistalumnirecord.utils.MyStringUtils;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Service;

/**
 * Created by zhao on 17-1-27.
 */
@Service
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUser> implements AdminUserService {

    @Override
    public AdminUser isLoginSuccess(AdminUser adminUser) {
        //判断是否为合法用户登录
        if (adminUser==null|| StringUtils.isNullOrEmpty(adminUser.getUsername())
                ||StringUtils.isNullOrEmpty(adminUser.getPassword())){
            //登录失败
            return null;
        }
        String md5Pw = MyStringUtils.getMd5String(adminUser.getPassword());
        adminUser.setPassword(md5Pw);
        AdminUser user = getDao().findById(adminUser.getUsername());
        if (adminUser==null||(!adminUser.getPassword().equals(md5Pw))){
            //登录密码错误
            return null;
        }
        //登录成功
        return user;
    }

    @Override
    public void save(AdminUser adminUser) {
        if (adminUser==null|| StringUtils.isNullOrEmpty(adminUser.getUsername())
                ||StringUtils.isNullOrEmpty(adminUser.getPassword())){
            //保存失败
            return ;
        }
        String md5pw = MyStringUtils.getMd5String(adminUser.getPassword());
        //md5加密
        adminUser.setPassword(md5pw);
        super.save(adminUser);
    }
}
